package com.longjianghuasheng.logistics.business.inquiry.adapter

import android.view.View
import android.widget.CheckBox
import android.widget.FrameLayout
import android.widget.TextView
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.business.inquiry.TYPE_2
import com.longjianghuasheng.logistics.business.inquiry.ThirdNode

class ThirdNodeProvider(var block: (ThirdNode, Int, Boolean) -> Unit) : BaseNodeProvider() {


    override val itemViewType: Int = TYPE_2

    override val layoutId: Int = R.layout.item_server2

    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        val tvTitle = helper.getView<TextView>(R.id.tv_title)
        tvTitle.text = (item as ThirdNode).title

        val checkBox = helper.getView<CheckBox>(R.id.check_box)

        checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
            item.isSelect = checkBox.isChecked
            if (!buttonView.isPressed) return@setOnCheckedChangeListener
            block.invoke(
                item,
                getAdapter()?.findParentNode(helper.absoluteAdapterPosition) ?: 0,
                isChecked
            )
        }
        checkBox.isChecked = item.isSelect

    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        super.onClick(helper, view, data, position)
        val checkBox = helper.getView<CheckBox>(R.id.check_box)
        checkBox.isPressed = true
        checkBox.isChecked = !checkBox.isChecked
    }
}